# -*- coding: utf-8 -*-
"""
-------------------------------------------------
   File Name：    encoder
   Author：       yubb
   date：         2021/4/19 10:38:55
   Description：  
  
-------------------------------------------------
"""
import json
from datetime import datetime, date
from decimal import Decimal

from bson import ObjectId
from pydantic import BaseModel


class DefaultEncoder(json.JSONEncoder):
    def default(self, o):
        """json序列化自定义编码器"""
        if isinstance(o, datetime):
            return o.strftime('%Y-%m-%d %X')
        elif isinstance(o, date):
            return o.strftime('%Y-%m-%d')
        elif isinstance(o, ObjectId):
            return str(o)
        elif isinstance(o, Decimal):
            return str(o)
        elif isinstance(o, BaseModel):
            return o.dict()
        else:
            return super().default(o)
